var getHref=window.location.href.split('?');
var typeStr=getHref[1].split('=')[1];
var sdType=Number(typeStr); //页面类型
var startStr="",endStr="";
var pickUpNameStr="请输入取货人姓名(选填)",pickUpTelStr="请输入取货人电话";
var receiverNameStr="请输入收货人姓名(选填)",receiverTelStr="请输入收货人电话";
var userInfo = localStorage.getItem("userInfo");//读取用户信息
var userInfoObj = JSON.parse(userInfo);
var userAddStr = sessionStorage.getItem("userAddMes"); //读取地址信息
if(userAddStr){
      var userAddObj = JSON.parse(userAddStr);
      $(".address1").val(userAddObj.QuHuoAddress);
      $(".address1").data("lat",userAddObj.Quhuo_Lat);
      $(".address1").data("lng",userAddObj.Quhuo_Lng);
      $(".address2").val(userAddObj.ShouhuoAddress);
      $(".address2").data("lat",userAddObj.Shouhuo_Lat);
      $(".address2").data("lng",userAddObj.Shouhuo_Lng);
      $('.pickup_tel').val(userAddObj.Quhuo_Tel);
      $('.receiver_tel').val(userAddObj.Shouhuo_Tel);
}          
    switch (sdType){
        // 买
        case 0:
              startStr="请输入购买地址";
              endStr="请输入收货地址";
              receiverTelStr="请输入联系电话";
               var nearBuy=sessionStorage.getItem('nearBuy');
                if(nearBuy=="0"){
                  $(".start_add").css("display","block");
                }else{
                  $(".start_add").css("display","none");
                }
                $('.address1').attr("placeholder",startStr);
                $('.address2').attr("placeholder",endStr);    
                $('.receiver_tel').attr("placeholder",receiverTelStr);
            break;
        //送
        case 1:
            startStr="请输入发货地址";
            endStr="请输入收货地址";
            $('.address1').attr("placeholder",startStr);
            $('.address2').attr("placeholder",endStr);
            $('#pickup_mes').css('display','block');
            break;
        //取
        case 2:
            startStr="请输入发货地址";
            endStr="请输入收货地址";
            $('.address1').attr("placeholder",startStr);
            $('.address2').attr("placeholder",endStr);
            $('#pickup_mes').css('display','block');
            break;
        //排队
        case 3:
            endStr="请输入排队地址";
            receiverNameStr="请输入联系姓名(选填)",
            receiverTelStr="请输入联系电话";
            $('.address2').attr("placeholder",endStr);
            $('.receiver_tel').attr("placeholder",receiverTelStr);
            $('.receiver_name').attr("placeholder",receiverNameStr);
            $('.address2_add').attr("placeholder","请补充详细地址，具体到楼层及门牌号");
            $('.start_add').css('display','none');
            break;
      }
 //声明地址的全局变量 
var QIAddress="",QILng,QILat;//起始地点  买 ，取 ，送
var ZhongAddress="",ZhongLng,ZhongLat;//终点
var get_la=[];//经纬数组
var addrAdd1="",addrAdd2="";//详细补充地址
var pickUpTel,pickUpName,receiverTel,receiverName;//收取货人信息
var startAdd="",endAdd="";//全地址信息
var addVal="";
var addSelect="";
      $(".address1").keyup(function(){
        addSelect=$(".address1");
        addVal=addSelect.val();
        ajaxAddressList();
      })
      $(".address2").keyup(function(){
        addSelect=$(".address2");
        addVal=addSelect.val();
        ajaxAddressList();
      });
      function ajaxAddressList(){
        $.ajax({
          type:"get",
          url:currentUrl+"/wxsdjs/addresshandler.ashx",
          data:{
            "query":addVal
          },
          dataType:"json",
          success:function(data){
            // console.log(data);
            addSelect.after($(".address_list"));
            $(".address_list").html("");
            $(".address_list").show();
            for(i=0;i<data.length;i++){
              var str="<li class='add_li'><i class='iconfont icon-dizhi'></i><span>"+data[i].city+data[i].district+data[i].name+"</span></li>";
              $(".address_list").append(str);
              if(i==data.length-1){
                var closeStr="<li class='addli_close'>关闭</li>"
                $(".address_list").append(closeStr);
              }
              $(".address_list li").eq(i).data("lat",data[i].location.lat);
              $(".address_list li").eq(i).data("lng",data[i].location.lng)
            };  
          },
          error:function(){
            console.log("获取不到地址数据")
          }
        })
      }

      $(".address_list").on("click",".add_li",function(){
        var $index=$(this).index();
        var $val=$(this).find("span").html();
        addSelect.val($val);
        addSelect.data("lng",$(this).data("lng"));
        addSelect.data("lat",$(this).data("lat"));
        // console.log(addSelect.data("lng"));
        $(".address_list").hide();
      })
      $(".address_list").on("click",".addli_close",function(){
        $(".address_list").hide();
      })

// 获得起点信息
function getStartAddMes(){
      QIAddress=$('.address1').val();
      QILng=$('.address1').data("lng");
      QILat=$('.address1').data("lat");      
      addrAdd1=$('.address1_add').val(); 
      // console.log("QILng:"+QILng);
      // console.log("QILat:"+QILat);
      if(QILat==null||QILng==null){
        console.log("起点信息错误");
      }   
}
// 获得终点信息
function getEndAddMes(){
      ZhongAddress=$('.address2').val();
      ZhongLng=$('.address2').data("lng");
      ZhongLat=$('.address2').data("lat"); 
      addrAdd2=$('.address2_add').val();
      if(ZhongLng==null||ZhongLat==null){
        console.log("收货信息错误");
      }   
}
//校验地址是否正确 成功后存储地址信息并返回上一页
function ajaxDisPrice(){
       $.ajax({
        type:"get",
        url:currentUrl+"/interfaceDOC/firstProject/User.ashx",
        data:{
          "action":"Get_OrderMoneyOne",
          "Quhuo_Lng":QILng,
          "Quhuo_Lat":QILat,
          "Shouhuo_Lng":ZhongLng,
          "Shouhuo_Lat":ZhongLat,
          "GoodsMoney":"0",
          "Flag":sdType,
        },
        dataType:"json",
        success:function(data){
          //console.log(data);
          if(data.msg=="成功"){
             setBuyAdd();//储存地址信息
             // console.log("money"+data.data.OrderMoney)
             window.location.href=document.referrer; //返回上一页; 
          }else{
            console.log(data)
            $.alert("请输入正确的地址");
            return false
          }
         
        },
        error:function(){
          $.alert("无法获得地址信息");
          return false
        }
      })
}
//存储收取货人参数信息 嵌入ajaxDisPrice()
function  setBuyAdd(){
        startAdd=QIAddress+addrAdd1;
        endAdd=ZhongAddress+addrAdd2;
        //是否就近买
        switch (sdType){
          case 0:
             if(nearBuy=="0"){
                var user_add={
                  "QuHuoAddress":startAdd,
                  "Quhuo_Lng":QILng,
                  "Quhuo_Lat":QILat,
                  "Quhuo_Person":pickUpName,
                  "Quhuo_Tel":pickUpTel,
                  "ShouhuoAddress":endAdd,
                  "Shouhuo_Lng":ZhongLng,
                  "Shouhuo_Lat":ZhongLat,
                  "Shouhuo_Person":receiverName, 
                  "Shouhuo_Tel":receiverTel,
                } 
              }else{
                 var user_add={
                  "QuHuoAddress":startAdd,
                  "Quhuo_Lng":ZhongLng,
                  "Quhuo_Lat":ZhongLat,
                  "Quhuo_Person":receiverName,
                  "Quhuo_Tel":receiverTel,
                  "ShouhuoAddress":endAdd,
                  "Shouhuo_Lng":ZhongLng,
                  "Shouhuo_Lat":ZhongLat,
                  "Shouhuo_Person":receiverName, 
                  "Shouhuo_Tel":receiverTel,
                } 
             }
          break;
          case 1:
               var user_add={
                  "QuHuoAddress":startAdd,
                  "Quhuo_Lng":QILng,
                  "Quhuo_Lat":QILat,
                  "Quhuo_Person":pickUpName,
                  "Quhuo_Tel":pickUpTel,
                  "ShouhuoAddress":endAdd,
                  "Shouhuo_Lng":ZhongLng,
                  "Shouhuo_Lat":ZhongLat,
                  "Shouhuo_Person":receiverName, 
                  "Shouhuo_Tel":receiverTel,
              } 
          break;
          case 2:
               var user_add={
                  "QuHuoAddress":startAdd,
                  "Quhuo_Lng":QILng,
                  "Quhuo_Lat":QILat,
                  "Quhuo_Person":pickUpName,
                  "Quhuo_Tel":pickUpTel,
                  "ShouhuoAddress":endAdd,
                  "Shouhuo_Lng":ZhongLng,
                  "Shouhuo_Lat":ZhongLat,
                  "Shouhuo_Person":receiverName, 
                  "Shouhuo_Tel":receiverTel,
              } 
          break;
          case 3:
          	 var user_add={
                  "QuHuoAddress":endAdd,
                  "Quhuo_Lng":ZhongLng,
                  "Quhuo_Lat":ZhongLat,
                  "Quhuo_Person":receiverName,
                  "Quhuo_Tel":receiverTel,
              };
             break;
      }  
        var userAddMes=JSON.stringify(user_add);
        sessionStorage.setItem("userAddMes",userAddMes);
}

//地址提交
  $("#address_submit").on("click",function(){
    address1=$('.address1').val();
    address2=$('.address2').val();
    pickUpTel=$('.pickup_tel').val();
    pickUpName=$('.pickup_name').val();
    receiverTel=$('.receiver_tel').val();
    receiverName=$('.receiver_name').val();
      switch (sdType){
        //买
        case 0:
            if(nearBuy=="0"){
                  if(address1==""){
                    $.alert(startStr);
                    return false
                  }else if(address2==""){
                    $.alert(endStr);
                    return false
                  }else if(receiverTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(receiverTel))){
                    $.alert("请输入正确的联系电话")
                    return false
                  }else{
                     getStartAddMes();
                     getEndAddMes();
                     ajaxDisPrice();
                  }
              }else if(nearBuy=="1"){//就近买计价问题--------！
                  if(address2==""){
                    $.alert(endStr);
                    return false
                  }else if(receiverTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(receiverTel))){
                    $.alert("请输入正确的联系电话")
                    return false
                  }else{
                    getEndAddMes();
                    QILng=$('.address2').data("lng");
                    QILat=$('.address2').data("lat"); 
                    ajaxDisPrice();
                  }

              }
            break;
        //送
        case 1:
            if(address1==""){
              $.alert(startStr);
              return false
            }else if(pickUpTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(pickUpTel))){
              $.alert(pickUpTelStr)
              return false
            }else if(address2==""){
              $.alert(endStr);
              return false
            }else if(receiverTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(receiverTel))){
              $.alert("请输入正确的收货电话")
              return false
            }else{
              getStartAddMes();
              getEndAddMes();
              ajaxDisPrice();
            }
            break;
        //取
        case 2:
            if(address1==""){
              $.alert(startStr);
              return false
            }else if(pickUpTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(pickUpTel))){
              $.alert(pickUpTelStr)
              return false
            }else if(address2==""){
              $.alert(endStr);
              return false
            }else if(receiverTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(receiverTel))){
              $.alert("请输入正确的收货电话")
              return false
            }else{
              getStartAddMes();
              getEndAddMes();
              ajaxDisPrice();
            }
            break;
        //排队
        case 3:
          if(address2==""){
              $.alert(endStr);
              return false
            }else if(receiverTel==""||!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(receiverTel))){
              $.alert("请输入正确的联系电话")
              return false
            }else{
              getEndAddMes();
              setBuyAdd();
              window.location.href=document.referrer; //返回上一页;同时刷新页面
            }
          break;
      }   
  })
